package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.display.BitmapData;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	import flash.utils.ByteArray;
	import flash.utils.getQualifiedClassName;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.graphics.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	public class PanelItem extends JPanel
	{
		public var icon_id:uint = 0;
		public var index:uint = 0;
		//public var userObject:Object = null;
		
		//members define
		public var panelImage:JPanel;
		public var labelCount:JLabel;
		private var _item:Object = null;
		public function PanelItem()
		{
			//component creation
			setSize(new IntDimension(45, 45));
			//setPreferredSize(new IntDimension(45, 45));
			var layout0:EmptyLayout = new EmptyLayout();
			setLayout(layout0);
			
			panelImage = new JPanel();
			panelImage.setLocation(new IntPoint(2, 2));
			panelImage.setSize(new IntDimension(40, 40));
			
			labelCount = new JLabel();
			labelCount.setLocation(new IntPoint(2, 28));
			labelCount.setSize(new IntDimension(41, 14));
			
			//component layoution
			append(panelImage);
			append(labelCount);
			
			addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			//addEventListener(MouseEvent.CLICK, onClick);
			addEventListener(ClickCountEvent.CLICK_COUNT, onItemClickCount);  
			
			//if(img_background == null || img_background_selected == null)
			//{
			//	img_background = new AssetBackground(Client.getUIElement("img_item_bg") as DisplayObject);
			//	img_background_selected = new AssetBackground(Client.getUIElement("img_item_bg_selected") as DisplayObject);
			//}
			if(showBackground)
				//setBackgroundDecorator(img_background);
				setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_item_bg") as DisplayObject));
			
			var filter:GlowFilter;
			var font:ASFont = new ASFont("宋体", 11, false, false, false, false);
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			labelCount.setFont(font);
			labelCount.setForeground(ASColor.WHITE);
			labelCount.setTextFilters(new Array(filter));
			labelCount.setHorizontalAlignment(AsWingConstants.RIGHT);
			//setClipMasked(false);
			//bringToTop(panelImage);
			//panelImage.setClipMasked(false);
			
		}
		public function clear():void
		{
			setBackgroundDecorator(null);
			panelImage.setBackgroundDecorator(null);
		}
		/*private var img_background:AssetBackground = null;
		private var img_background_selected:AssetBackground = null;
		private function onRollOver(event:MouseEvent):void
		{
			if(getBackgroundDecorator() != img_background_selected && showBackground)
				setBackgroundDecorator(img_background_selected);
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			if(getBackgroundDecorator() != img_background && showBackground)
				setBackgroundDecorator(img_background);
		}*/
		
		private var emptyBorder:EmptyBorder = new EmptyBorder();
		private var lineBorder:LineBorder = new LineBorder(null,ASColor.YELLOW);
		private var filterArray:Array = new Array(new GlowFilter(0xFFFF00, 1, 2, 2, 3));
		private function onRollOver(event:MouseEvent):void
		{
			if(_showBackground)
			{
				if(filters != filterArray)
					filters = filterArray;
			}
			else
			{
				if(getBorder() != lineBorder)
					setBorder(lineBorder);
			}
			
			if(this.parent is Container)
				(this.parent as Container).bringToTop(this);
			FrameClickItem.hide();
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			if(_showBackground)
			{
				filters = null;
			}
			else
			{
				if(getBorder() != emptyBorder)
					setBorder(emptyBorder);
			}
			//FrameClickItem.hide();
		}
		
		private function onItemClickCount(event:ClickCountEvent):void
		{   
			if(event.getCount() == 1)
			{   
				if(_item != null && _item.csv_id != 0 && _enableClick)
				{
					var point:Point = this.localToGlobal(new Point(width/2,height/2));
					var pointParent:Point = (this.parent as JPanel).globalToLocal(point);
					//FrameClickItem.show(this.parent as JPanel,x + width/2, y + height/2);
					FrameClickItem.show(this.parent as JPanel,pointParent.x, pointParent.y);
					FrameClickItem._item = _item;
					return;
				}
			}   
			FrameClickItem.hide();
		} 
		
		public function enableUse():Boolean
		{
			var enableUse:Boolean = true;
			var objItemCSV:Object = Client.getInstance().GetItemCSV(_item.csv_id);
			if(objItemCSV != null)
			{
				if(objItemCSV.require_level > 0)
				{
					if(CharInfo.m_nLevel < objItemCSV.require_level)
					{
						enableUse = false;
					}
				}
				
				if(int(objItemCSV.require_race) != -1)
				{
					if(CharInfo.m_nRace != objItemCSV.require_race)
					{
						enableUse = false;
					}
					
				}
				
				if(objItemCSV.require_gender == 0 || objItemCSV.require_gender == 1)
				{
					if(CharInfo.m_nGender != objItemCSV.require_gender)
					{
						enableUse = false;
					}
	
				}
			}
			return enableUse;
		}
		
		public function isEmpty():Boolean
		{
			if(_item == null)
				return true;
			if(_item.csv_id <= 0)
				return true;
			return false;
		}
		//private var filterArrayImage:Array = new Array(new GlowFilter(0xEA0000, 0.6, 34, 34, 2,1,true));
		private var filterArrayImage:Array = new Array(new GlowFilter(0xEA0000, 1, 2, 2, 10));
		
		private var _tooltip:ToolTipItem = new ToolTipItem();
		public function set item(val:Object):void
		{
			_item = val;
			_tooltip.setTargetComponent(null);
			//panelImage.setBackgroundDecorator(null);
			labelCount.setText("");
			panelImage.filters = null;
			if(_item == null)
			{
				icon_id = 0;
				panelImage.setBackgroundDecorator(null);
				return;
			}
			if(_item.csv_id <= 0)
			{
				icon_id = 0;
				panelImage.setBackgroundDecorator(null);
				return;
			}
			var objItemCSV:Object = Client.getInstance().GetItemCSV(_item.csv_id);
			if(objItemCSV == null)
			{
				icon_id = 0;
				panelImage.setBackgroundDecorator(null);
				return;
			}
			
			_tooltip.setItem(_item);
			_tooltip.setTargetComponent(this);
			if(icon_id != objItemCSV.icon_id)
			{
				icon_id = objItemCSV.icon_id;
				var dis:DisplayObject = Client.getItemElement("img_item_"+ objItemCSV.icon_id) as DisplayObject;
				panelImage.setBackgroundDecorator(dis != null ? new AssetBackground(dis):null);
			}
			labelCount.setText(_item.count > 1 ? _item.count.toString() : "");
			
			panelImage.setSize(new IntDimension(this.width-4, this.height-4));
			panelImage.setPreferredSize(new IntDimension(this.width-4, this.height-4));
			panelImage.filters = enableUse() ? null : filterArrayImage;
			panelImage.pack();
			panelImage.paintImmediately();
			pack();
			this.paintImmediately();
		}
		public function get item():Object
		{
			return _item;
		}
		private var _showBackground:Boolean = true;
		public function set showBackground(show:Boolean):void
		{
			_showBackground = show;
			setBackgroundDecorator(null);
			setBackgroundDecorator(_showBackground ? new AssetBackground(Client.getUIElement("img_item_bg") as DisplayObject) : null);
		}
		public function get showBackground():Boolean
		{
			return _showBackground;
		}
		public static function hideClick():void
		{
			FrameClickItem.hide();
		}
		
		private var _enableClick:Boolean = true;
		public function set enableClick(click:Boolean):void
		{
			_enableClick = click;
		}
		public function get enableClick():Boolean
		{
			return _enableClick;
		}
		
	}
}

import com.*;
import com.Engine.*;
import com.Packet.*;
import com.UI.*;

import flash.display.*;
import flash.events.*;
import flash.external.*;
import flash.filters.*;
import flash.geom.*;
import flash.system.*;
import flash.text.*;
import flash.ui.*;
import flash.utils.*;

import org.aswing.*;
import org.aswing.border.*;
import org.aswing.border.EmptyBorder;
import org.aswing.colorchooser.*;
import org.aswing.dnd.*;
import org.aswing.event.*;
import org.aswing.ext.*;
import org.aswing.geom.*;

/**
 * MyPaneClickItem
 */
class FrameClickItem extends JFrame{
	
	//members define
	private var btn_use:JLabelButton;
	private var btn_equip:JLabelButton;
	private var btn_stall:JLabelButton;
	private var btn_split:JLabelButton;
	private var btn_discard:JLabelButton;
	private var btn_close:JLabelButton;
	
	
	/**
	 * MyPaneClickItem Constructor
	 */
	public function FrameClickItem(){
		
		setResizable(false);
		setClosable(false);
		setDragable(false);
		setBackgroundDecorator(null);
		setTitleBar(null);

		//this.getTitleBar().setMinimizeHeight(0);
		
		//component creation
		setSize(new IntDimension(60, 105));
		setPreferredSize(new IntDimension(60, 105));
		var border0:LineBorder = new LineBorder();
		border0.setInterior(null);
		border0.setColor(new ASColor(0x0, 1));
		border0.setThickness(1);
		border0.setRound(0);
		setBorder(border0);
		var layout1:BoxLayout = new BoxLayout();
		layout1.setAxis(AsWingConstants.VERTICAL);
		layout1.setGap(0);
		getContentPane().setLayout(layout1);
		
		btn_use = new JLabelButton();
		btn_use.setLocation(new IntPoint(5, 5));
		btn_use.setSize(new IntDimension(28, 17));
		btn_use.setText("使用");
		
		btn_equip = new JLabelButton();
		btn_equip.setLocation(new IntPoint(5, 27));
		btn_equip.setSize(new IntDimension(26, 17));
		btn_equip.setText("穿戴");
		
		btn_stall = new JLabelButton();
		btn_stall.setLocation(new IntPoint(1, 29));
		btn_stall.setSize(new IntDimension(58, 14));
		btn_stall.setText("上架");
		
		btn_split = new JLabelButton();
		btn_split.setLocation(new IntPoint(0, 66));
		btn_split.setSize(new IntDimension(60, 33));
		btn_split.setText("拆分");
		
		btn_discard = new JLabelButton();
		btn_discard.setLocation(new IntPoint(0, 75));
		btn_discard.setSize(new IntDimension(60, 25));
		btn_discard.setText("丢弃");
		
		btn_close = new JLabelButton();
		btn_close.setLocation(new IntPoint(0, 80));
		btn_close.setSize(new IntDimension(60, 20));
		btn_close.setText("关闭");
		
		//component layoution
		getContentPane().append(btn_use);
		getContentPane().append(btn_equip);
		getContentPane().append(btn_stall);
		getContentPane().append(btn_split);
		getContentPane().append(btn_discard);
		getContentPane().append(btn_close);
		
		btn_use.setForeground(ASColor.WHITE);
		btn_use.setRollOverColor(ASColor.YELLOW);
		btn_use.setPressedColor(ASColor.BLUE);
		btn_equip.setForeground(ASColor.WHITE);
		btn_equip.setRollOverColor(ASColor.YELLOW);
		btn_equip.setPressedColor(ASColor.BLUE);
		btn_stall.setForeground(ASColor.WHITE);
		btn_stall.setRollOverColor(ASColor.YELLOW);
		btn_stall.setPressedColor(ASColor.BLUE);
		btn_split.setForeground(ASColor.WHITE);
		btn_split.setRollOverColor(ASColor.YELLOW);
		btn_split.setPressedColor(ASColor.BLUE);
		btn_discard.setForeground(ASColor.WHITE);
		btn_discard.setRollOverColor(ASColor.YELLOW);
		btn_discard.setPressedColor(ASColor.BLUE);
		btn_close.setForeground(ASColor.WHITE);
		btn_close.setRollOverColor(ASColor.YELLOW);
		btn_close.setPressedColor(ASColor.BLUE);
		
		setBackgroundDecorator(new SolidBackground(new ASColor(0x0e3c3b,1.0)));
		
		btn_use.addActionListener(onBtnUse);
		btn_equip.addActionListener(onBtnUse);
		btn_stall.addActionListener(onBtnStall);
		btn_split.addActionListener(onBtnSplit);
		btn_close.addActionListener(onBtnClose);
		btn_discard.addActionListener(onBtnDiscard);
	}
	private function onBtnStall(e:Event):void
	{
		//this.closeReleased();
		FrameStall.showTakeOn(_item);
		hide();
	}
	private function onBtnClose(e:Event):void
	{
		//this.closeReleased();
		hide();
	}
	private function onBtnDiscard(e:Event):void
	{
		//this.closeReleased();
		hide();
		var objItemCSV:Object = Client.getInstance().GetItemCSV(_item.csv_id);
		var pane:FrameInfo = FrameInfo.showMessageDialog("","是否丢弃物品 "+objItemCSV.name+" ?",handleOptionRemoveItem,null,true,null,JOptionPane.YES|JOptionPane.NO);
	}
	private static function handleOptionRemoveItem(button:uint):void
	{
		if(button == JOptionPane.YES)
		{
			Client.getInstance().sendPacketLogic(16,[_item.page,_item.index]);
		}
	}
	private function onBtnUse(event:Event):void
	{   
		hide();
		Client.getInstance().sendPacketLogic(18,[_item.page,_item.index]);
	}
	
	private function onBtnSplit(e:Event):void
	{
		hide();
		FrameInfo.showInputDialog("","请输入要拆分出来的个数(1-"+(_item.count-1)+'):',handleOptionSplit,"",null,true,null);
	}
	
	private function handleOptionSplit(inputText:String):void
	{
		if(inputText !=	null)
		{
			if(inputText.length <= 0 || int(inputText) == 0)
			{
				FrameInfo.showMessageDialog("","输入的数目不正确",null,null,true,null);
				return;
			}
			Client.getInstance().sendPacketLogic(50,[_item.page,_item.index,int(inputText)]);
		}
		
	}
	
	public static var _frame:FrameClickItem = null;
	public static var _item:Object = null;
	public static function show(parent:JPanel,x:int,y:int):void
	{
		if(_frame == null)
		{
			_frame = new FrameClickItem();
			//parent.addChild(_frame);
		}
		parent.addChild(_frame);
		_frame.show();
		parent.bringToTop(_frame);

		_frame.setLocationXY(x, y);
		
	}
	public static function hide():void
	{
		if(isShowing())_frame.hide();
		_item = null;
	}
	public static function isShowing():Boolean
	{
		return(_frame != null&& _frame.isShowing());
	}
	
}